#include<iostream>
#include<cmath>
using namespace std;
long long l[20005],r[20005];
long long f[20005][2];
int dis(int a,int b){return abs(a-b);}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>l[i]>>r[i];
    }
    f[1][0]=dis(1,r[1])+dis(l[1],r[1]);
    f[1][1]=dis(1,r[1]);
    for(int i=2;i<=n;i++){
        f[i][1]=min(f[i-1][1]+dis(r[i-1],l[i])+dis(l[i],r[i]),f[i-1][0]+dis(l[i-1],l[i])+dis(l[i],r[i]))+1;
         f[i][0]=min(f[i-1][0]+dis(l[i-1],r[i])+dis(l[i],r[i]),f[i-1][1]+dis(r[i-1],r[i])+dis(l[i],r[i]))+1;
    }
    cout<<min(f[n][1]+dis(r[n],n),f[n][0]+dis(l[n],n));
    return 0;
}